﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using AudioCore.Entity;
using AudioCore.Biz;
using AudioCore.Common;
using AudioBook.AudioManagement.Util;

namespace AudioBook.AudioManagement.CategoryManage
{
    public partial class AddCategory : System.Web.UI.Page
    {
        #region Fields & constants

        CategoryBiz bizCategory = new CategoryBiz();
        List<CategoryEntity> lstCategory = new List<CategoryEntity>();
        #endregion Fields & constants

        #region Private methods

        private void LoadData()
        {
            LoadCategory();
        }

        private void LoadCategory()
        {
                dropParent.Items.Clear();
                List<CategoryEntity> lstCatTemp = bizCategory.GetAllCategory();
                lstCatTemp.Sort(delegate(CategoryEntity en1, CategoryEntity en2) { return en2.SortOrder.CompareTo(en1.SortOrder); });
                ProcessCategory(lstCatTemp, 0, string.Empty);
        }

        private void ProcessCategory(List<CategoryEntity> lstParent, int parentID, string space)
        {
            List<CategoryEntity> lstChild = lstParent.Where(item => item.ParentID == parentID).ToList<CategoryEntity>();
            foreach (CategoryEntity item in lstChild)
            {
                item.CategoryName = space + item.CategoryName;
                dropParent.Items.Add(new ListItem(item.CategoryName, item.CategoryID.ToString()));
                ProcessCategory(lstParent, item.CategoryID, space + "---+");
            }
        }

        private bool CheckValid()
        {
            if (string.IsNullOrEmpty(txtCategoryName.Text))
            {
                valCommon.IsValid = false;
                valCommon.ErrorMessage = "The title could not be empty.";
                return false;
            }
            return true;
        }

        private int SaveCategory()
        {
            if (CheckValid())
            {
                CategoryEntity enCategory = new CategoryEntity();
                enCategory.ParentID = int.Parse(dropParent.SelectedValue);
                enCategory.CategoryName = txtCategoryName.Text;
                if (fulImage.HasFile)
                {
                    enCategory.HasImage = true;
                }
                enCategory.LastTimeUpdate = DateTime.Now;
                enCategory.Actived = chkActive.Checked;
                enCategory.Description = txtDescription.Text;
                enCategory.CreatedDateTime = DateTime.Now;
                enCategory.SortOrder = DateTime.Now;

                int result = bizCategory.AddCategory(enCategory);
                if (result > 0)
                {
                    string strImg = GlobalParam.DataFolder + Constant.CATEGORY_FOLDER + result.ToString() + Constant.IMAGE_EXTENSION;
                    fulImage.SaveAs(strImg);
                }
                else
                {
                    valCommon.IsValid = false;
                    valCommon.ErrorMessage = "Transaction error, the category is not inserted successfully.";
                }
                return result;
            }
            else
            {
                return 0;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void dropModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCategory();
        }

        protected void btnSaveAndClose_Click(object sender, EventArgs e)
        {
            int result = SaveCategory();
            if (result > 0)
            {
                Response.Redirect(GUIConstants.WebPage.CategoryPage.DefaultCategory);
            }
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int result = SaveCategory();
            if (result > 0)
            {
                txtCategoryName.Text = string.Empty;
                txtDescription.Text = string.Empty;
                chkActive.Checked = true;
            }
        }
    }
}
